using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PeaBullet : Plants
{
    public Vector3 direction; //发射方向
    public float speed;//速度
    public float existenceTime = 10f;//存在时间
    public int damage;//伤害
    private Animator animator;
    bool isFire = false;

    private void Start()
    {
        animator = GetComponent<Animator>();
        StartCoroutine(SetDestroy());
    }

    void Update()
    {
        transform.position += direction * speed * Time.deltaTime;
    }

    IEnumerator SetDestroy()
    {
        yield return new WaitForSeconds(existenceTime); // 等待interval秒
        Destroy(gameObject);
    }

    //进入触发器 
    private void OnTriggerEnter2D(Collider2D other)
    {
        
        //触碰火炬树桩变为火豆,伤害翻倍
        if (other.gameObject.GetComponent<Torchwood>())
        {
            animator.SetBool("isFire", true);
            damage = damage * 2;
            isFire = true;
        }

        if (other.CompareTag("Enemy"))
        {
            other.GetComponent<Character>()?.TakeDamage(damage);
            AudioManager.Instance.PlaySFX("豌豆击中");
            //生成火焰效果
            if(isFire){
                GameObject firePrefab = Resources.Load("Prefabs/特效/火焰特效") as GameObject;
                GameObject go = Instantiate(firePrefab);
                go.transform.parent =  other.gameObject.transform;
                go.transform.localPosition = Vector2.zero + new Vector2(0, -25);
            }
        }
    }
}
